package com.grt.common.log;

import java.util.TimerTask;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.grt.login.dao.LoginLogDao;
import com.grt.login.domain.LoginLog;

@Component
public class LogTaskFactory {

	private static final Logger logger = LoggerFactory.getLogger(LogTaskFactory.class);

	@Autowired
	private LoginLogDao aloginLogDao;

	private static LoginLogDao loginLogDao;

	@PostConstruct // 容器实例化当前受管Bean时，@PostConstruct注解的方法会被自动触发，
	// 从而完成一些初始化工作。由于static修饰的属性是类属性，不是对象属性，所以spring不允许自动注入,只能采用这种取巧的方法来进行
	public void init() {
		loginLogDao = aloginLogDao;
	}

	public static TimerTask getOperationSuccessTimerTask(long userid, String className, String logName,
			String methodNmae, String message) {
		return new TimerTask() {

			@Override
			public void run() {
				String msg = userid + LogConstants.LOGATT_SPLIT_CHAR + className + LogConstants.LOGATT_SPLIT_CHAR
						+ logName + LogConstants.LOGATT_SPLIT_CHAR + LogConstants.LOGTYPE_BUSSINESS
						+ LogConstants.LOGATT_SPLIT_CHAR + message + LogConstants.LOGATT_SPLIT_CHAR + methodNmae
						+ LogConstants.LOGATT_SPLIT_CHAR + LogConstants.LOGSTATE_SUCCESSED;

				logger.info(msg);// 操作日志各个属性之间用@分割

			}
		};
	}

	public static TimerTask getOperationFailTimerTask(long userid, String className, String methodNmae,
			String message) {
		return new TimerTask() {

			@Override
			public void run() {

				String msg = userid + LogConstants.LOGATT_SPLIT_CHAR + className + LogConstants.LOGATT_SPLIT_CHAR + null
						+ LogConstants.LOGATT_SPLIT_CHAR + LogConstants.LOGTYPE_BUSSINESS
						+ LogConstants.LOGATT_SPLIT_CHAR + message + LogConstants.LOGATT_SPLIT_CHAR + methodNmae
						+ LogConstants.LOGATT_SPLIT_CHAR + LogConstants.LOGSTATE_FAILED;

				logger.info(msg);// 操作日志各个属性之间用@分割
			}
		};
	}

	public static TimerTask getLoginSuccessTimerTask(long userid, String username, String ip) {
		return new TimerTask() {

			@Override
			public void run() {
				LoginLog loginLog = LogFactory.newLoginLogIntstance(userid, LogConstants.LOGTYPE_LOGIN,
						LogConstants.LOGSTATE_SUCCESSED, username, ip);
				try {
					loginLogDao.save(loginLog);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}

	/**
	 * 登录日志Task
	 * 
	 * @param username
	 * @param message
	 * @param ip
	 * @return
	 */
	public static TimerTask getLoginFailTimerTask(String username, String message, String ip) {
		return new TimerTask() {

			@Override
			public void run() {
				LoginLog loginLog = LogFactory.newLoginLogIntstance(null, LogConstants.LOGTYPE_LOGIN,
						LogConstants.LOGSTATE_FAILED, "用户名：" + username + ",原因：" + message, ip);
				try {
					loginLogDao.save(loginLog);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}

	/**
	 * 登出日志Task
	 * 
	 * @param userid
	 * @param username
	 * @param ip
	 * @return
	 */
	public static TimerTask getLogoutTimerTask(long userid, String username, String ip) {
		return new TimerTask() {

			@Override
			public void run() {
				LoginLog loginLog = LogFactory.newLoginLogIntstance(userid, LogConstants.LOGTYPE_LOGOUT,
						LogConstants.LOGSTATE_SUCCESSED, username, ip);
				try {
					loginLogDao.save(loginLog);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
}
